package com.pointstar.dp.modules.project.service.impl;

import com.pointstar.dp.common.entity.Page;
import com.pointstar.dp.common.entity.Query;
import com.pointstar.dp.common.entity.R;
import com.pointstar.dp.common.utils.CommonUtils;
import com.pointstar.dp.common.utils.ShiroUtils;
import com.pointstar.dp.modules.project.dao.InBoundDetailsMapper;
import com.pointstar.dp.modules.project.dao.InBoundMapper;
import com.pointstar.dp.modules.project.entity.InBoundDetailsEntity;
import com.pointstar.dp.modules.project.entity.InBoundEntity;
import com.pointstar.dp.modules.project.entity.ro.SaveInBoundRo;
import com.pointstar.dp.modules.project.service.InBoundService;
import com.pointstar.dp.modules.project.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 入库主表
 * @author Korey<m13057531285@163.com>
 */
@Service("inBoundService")
public class InBoundServiceImpl implements InBoundService {

	private final InBoundMapper inBoundMapper;

	private final InBoundDetailsMapper inBoundDetailsMapper;

	@Autowired
	public InBoundServiceImpl(InBoundMapper inBoundMapper, InBoundDetailsMapper inBoundDetailsMapper) {
		this.inBoundMapper = inBoundMapper;
		this.inBoundDetailsMapper = inBoundDetailsMapper;
	}

	/**
     * 分页查询
     * @param params
     * @return
     */
	@Override
	public Page<InBoundEntity> listInBound(Map<String, Object> params) {
		Query query = new Query(CommonUtil.setTime(params));
		Page<InBoundEntity> page = new Page<>(query);
		inBoundMapper.listForPage(page, query);
		return page;
	}

    /**
     * 新增
     * @param inBound
     * @return
     */
	@Override
	public R saveInBound(InBoundEntity inBound) {
		int count = inBoundMapper.save(inBound);
		return CommonUtils.msg(count);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R saveInBoundAndDetails(SaveInBoundRo saveInBoundRo) {
		BigDecimal bigDecimal = new BigDecimal("0");
        for (InBoundDetailsEntity inBoundDetailsEntity : saveInBoundRo.getInBoundDetailsEntities()) {
            bigDecimal = bigDecimal.add(inBoundDetailsEntity.getTotalPrice());
        }
		InBoundEntity inBoundEntity = InBoundEntity.builder()
				.inBoundNumber(saveInBoundRo.getInBoundNumber())
				.supplier(saveInBoundRo.getSupplier())
				.inTypeCode(saveInBoundRo.getInTypeCode())
				.inDeptCode(saveInBoundRo.getInDeptCode())
				.inItemCode(saveInBoundRo.getInItemCode())
				.inBoundPeople(ShiroUtils.getUserEntity().getRealName())
				.amount(bigDecimal).build();
		int countInBound = inBoundMapper.save(inBoundEntity);
		if (inBoundEntity.getId() == null) {
			throw new RuntimeException("商品入库操作失败!");
		}
		saveInBoundRo.getInBoundDetailsEntities()
				.forEach(inBoundDetailsEntity -> {
					inBoundDetailsEntity.setInBoundId(inBoundEntity.getId());
					if (inBoundDetailsMapper.save(inBoundDetailsEntity) <= 0) {
						throw new RuntimeException("商品入库明细操作失败!");
					}
				});
		return CommonUtils.msg(countInBound);
	}

	/**
     * 根据id查询
     * @param id
     * @return
     */
	@Override
	public R getInBoundById(Long id) {
		InBoundEntity inBound = inBoundMapper.getObjectById(id);
		return CommonUtils.msg(inBound);
	}

    /**
     * 修改
     * @param inBound
     * @return
     */
	@Override
	public R updateInBound(InBoundEntity inBound) {
		int count = inBoundMapper.update(inBound);
		if (count == 0) {
			return R.error("入库单中已有商品出库,无法修改!!！");
		}
		return CommonUtils.msg(count);
	}

    /**
     * 删除
     * @param id
     * @return
     */
	@Override
	public R batchRemove(Long[] id) {
		int count = inBoundMapper.batchRemove(id);
		return CommonUtils.msg(id, count);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R batchRemoveLogic(Long[] id) {
		//更新入库主表的delete_flag
		int count = inBoundMapper.updateDeleteFlag(id);

		if (count == 0) {
			return R.error("入库单中已有商品出库,无法作废!");
		}
		//往入库主表插入负记录
		int count2 = inBoundMapper.saveNegativeRecord(id);

		//更新入库明细表的delete_flag
		int count3 = inBoundDetailsMapper.updateDeleteFlagByInBoundId(id);

		return CommonUtils.msg(id, count2);

	}

}
